<?php

class Manager extends Controller {

	function Manager()
	{
		parent::Controller();

		$this->db->query('set names utf8');

		$this->authlib->check();
	}

	// 初始化新增对话框与编辑对话框中的下拉列表框等的数据
	function _initSystemparam()
	{
		$this->load->model('Districtmodel');
		$this->load->model('Systemparam');

		// 性别下拉列表
		$data['sex'] = $this->Systemparam->getParamByParentId(68);
		// 类别下拉列表
		$data['req_sort'] = $this->Systemparam->getParamByParentId(30);
		// 等级下拉列表
		$data['req_grade'] = $this->Systemparam->getParamByParentId(31);
		// 景观下拉列表
		$data['scene'] = $this->Systemparam->getParamByParentId(27);
		// 城区下拉列表
		$data['city_zone'] = $this->Districtmodel->getDistrictByLevel(1);
		// 用途下拉列表
		$data['purpose'] = $this->Systemparam->getParamByParentId(7);
		// 朝向下拉列表
		$data['house_dir'] = $this->Systemparam->getParamByParentId(11);
		// 客源状态下拉列表
		$data['status'] = $this->Systemparam->getParamByParentId(28);
		// 装修下拉列表
		$data['fitment'] = $this->Systemparam->getParamByParentId(12);
		// 客源来源下拉列表
		$data['source'] = $this->Systemparam->getParamByParentId(29);
		// 付佣下拉列表
		$data['pay_squeeze'] = $this->Systemparam->getParamByParentId(10);
		// 付款下拉列表
		$data['pay_mode'] = $this->Systemparam->getParamByParentId(13);
		// 房间配置列表
		$data['ptinfo'] = $this->Systemparam->getParamByParentId(25);
		// 配套设施列表
		$data['buildinfo'] = $this->Systemparam->getParamByParentId(55);

		return $data;
	}
	
	
	// 构建客源管理界面
	function index()
	{
		$this->load->model('Systemparam');

		// 用途下拉列表
		$data['yongtu'] = $this->Systemparam->getParamByParentId(7);
		$this->load->view('customer/manager', $data);
	}
	
	// 根据条件查询客源信息，返回XML
	function search()
	{
		$this->load->helper('xml');
		$this->load->model('Customermodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
		
		$output = '<?xml version="1.0" encoding="UTF-8" ?>';
		//if (!empty($_POST['data']))
		//	parse_str($_POST['data'], $condition);
		// 取出分页之前的总数
		$num = $this->Customermodel->search($_POST, true);
		$num = $num[0]->count;
		$data = $this->Customermodel->search($_POST);
		$output .= "<SearchReponse>";
		if (!empty($data))
		{
			$output .= "<total>" . $num . "</total>\n";
			foreach ($data as $item)
			{
				$output .= "<Items>\n";
				$output .= "<Item>\n";
				foreach ($item as $key => $value)
				{
					$output .= "<$key>".xml_convert($value)."</$key>\n";
				}
				$output .= "<house_type>{$item->house_typedown}-{$item->house_typeup}户</house_type>\n";
				if ($item->if_rent=='求租')
				{
					$price_unit = '元';
				}
				else
				{
					$price_unit = '万';
				}
				$output .= "<price>{$item->price_down}-{$item->price_up}$price_unit</price>\n";
				$output .= "<area>{$item->area_down}-{$item->area_up}㎡</area>\n";
				$output .= "</Item>\n";
				$output .= "</Items>";
			}
		}
		$output .= '</SearchReponse>';

		$this->output->set_output($output);
	}

	// 根据地区上级ID查询地区信息，返回XML
	function getDistrict($pid)
	{
		$this->load->model('Districtmodel');

		$output = $this->Districtmodel->getDistrictXml($pid);

		$this->output->set_output($output);
	}

	// 根据顶级ID查询下级所有地区信息，返回XML
	function getAllDistrict($id)
	{
		$this->load->model('Districtmodel');

		$output = $this->Districtmodel->getAllDistrictXml($id);

		$this->output->set_output($output);
	}

	// 显示客源相信信息，返回HTML
	function detail($id)
	{
		$this->load->model('Customermodel');

		$data['detail'] = $this->Customermodel->getCustomerById($id);

		$data['if_rent1'] = '';
		$data['if_rent2'] = '';
		$data['ky_secret'] = '';
		$data['kd_secret'] = '';

		if ($data['detail']->if_rent=='求租')
		{
			$price_unit = '元';
			$data['if_rent1'] = 'checked="checked"';
		}
		else
		{
			$price_unit = '万';
			$data['if_rent2'] = 'checked="checked"';
		}

		$data['house_type'] = "{$data['detail']->house_typedown}-{$data['detail']->house_typeup}户";
		$data['price'] = "{$data['detail']->price_down}-{$data['detail']->price_up}$price_unit";
		$data['area'] = "{$data['detail']->area_down}-{$data['detail']->area_up}㎡";
		if ($data['detail']->ky_secret == 1)
		{
			$data['ky_secret'] = 'checked="checked"';
		}
		if ($data['detail']->kd_secret == 1)
		{
			$data['kd_secret'] = 'checked="checked"';
		}

		$this->load->view('customer/manager_detail', $data);
	}

	// 新增客源
	function add()
	{
		$data = $this->_initSystemparam();
		$data['mode'] = 'add';
		$data['editdata'] = '';

		$this->load->view('customer/manager_dialog', $data);
	}

	// 保存新增客源
	function addsave()
	{
		$this->load->model('Customermodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		//if (!empty($_POST['data']))
		//	parse_str($_POST['data'], $condition);
		$data = $this->Customermodel->add($_POST);

		$output = '<?xml version="1.0"?><response><SaveResponse>';
		if ($data==true)
		{
			$output .= 'true';
		}
		else
		{
			$output .= 'false';
		}
		$output .= '</SaveResponse></response>';
		$this->output->set_output($output);
	}

	// 编辑客源
	function edit($id)
	{
		$this->load->model('Customermodel');

		$data = $this->_initSystemparam();
		$data['mode'] = 'edit';
		$result = $this->Customermodel->getCustomerById($id);
		$result->city_zone_id = $this->Districtmodel->getDistrictIdByName(0, $result->city_zone);
		$result->region_zone_id = $this->Districtmodel->getDistrictIdByName($result->city_zone_id, $result->region_zone);
		$result->house_zone_id = $this->Districtmodel->getDistrictIdByName($result->region_zone_id, $result->house_zone);
		$data['editdata'] = $result;

		$this->load->view('customer/manager_dialog', $data);
	}

	// 保存编辑客源
	function editsave()
	{
		$this->load->model('Customermodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		//if (!empty($_POST['data']))
		//	parse_str($_POST['data'], $condition);
		$data = $this->Customermodel->edit($_POST);

		$output = '<?xml version="1.0"?><response><SaveResponse>';
		if ($data==true)
		{
			$output .= 'true';
		}
		else
		{
			$output .= 'false';
		}
		$output .= '</SaveResponse></response>';
		$this->output->set_output($output);
	}
	
	// 删除客源
	function delete()
	{
		$this->load->model('Customermodel');

		// Load the mime types
		@include(APPPATH.'config/mimes'.EXT);

		header("Content-Type: {$mimes['xml']};");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");

		$param = explode(",", $_POST['data']);
		$data = $this->Customermodel->delete($param);

		$output = '<?xml version="1.0"?><response><SaveResponse>';
		if ($data==true)
		{
			$output .= 'true';
		}
		else
		{
			$output .= 'false';
		}
		$output .= '</SaveResponse></response>';
		$this->output->set_output($output);
		
	}
}
?>